package server;
import java.io.IOException;

import Client.Game;
import Client.Turn;

import com.lloseng.ocsf.server.AbstractServer;
import com.lloseng.ocsf.server.ConnectionToClient;

/**
 * server
 * send and reciever message with client
 * @author Termchai 5510546042, Eknarin 5510546239
 *
 */
public class Server extends AbstractServer {

	private static Client.Game game;
	private static int count;
	private static int maxConnection;
	private static Turn turn;
	private Client.MesSelect Mes;
	public Server(int port) {
		super(port);
		// TODO Auto-generated constructor stub
		System.out.println("Open Server");
		turn=turn.P1;
		game = new Client.Game();
	}

	@Override
	protected void handleMessageFromClient(Object msg, ConnectionToClient client) {
		// TODO Auto-generated method stub
		System.out.println("hadle from server");
		//if (msg.getClass() == Mes.getClass() )
		//{
			Client.MesSelect mes = (Client.MesSelect)msg;
			sendToAllClients(mes);
		//}
	}
	
	protected void clientConnected(ConnectionToClient client) {
		// TODO Auto-generated method stub
		super.clientConnected(client);
		count++;
		System.out.println("has connect " +count);
		if (count==1)
		{
			client.setInfo("Player", Turn.P1);
			try {
				client.sendToClient("P1");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (count==2)
		{
			client.setInfo("Player", Turn.P2);

			try {
				client.sendToClient("P2");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			sendToAllClients(game);
			
			
		}
		System.out.println(count+"");
		
		
		System.out.println(client.getInfo("Player")+" has connect.");
		
	}
	
	
}
